﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using VersionComparer.Base;
using VersionComparer.Configuration;
using VersionComparer.UI;
using VersionComparer.Views;

namespace VersionComparer.DataModel.Actions
{
    [ActionAttribute("Compare",30, System.Windows.Forms.Keys.F1)]
    public class RichTextComparisonAction : SpecificAction
    {
        public RichTextComparisonAction()
            : base()
        {
        }

        public override bool RefreshAfterFinish
        {
            get
            {
                return false;
            }
            set
            {
            }
        }
        public override bool MayBeExecuted(MainForm mainForm, ComparableItem[] focusedSelectedItems, ComparableItem[] unfocusedSelectedItems)
        {
            if (focusedSelectedItems.Length == 0)
                return false;
            return true;
        }


        private int CountLines(string text)
        {
            if (text == null)
                return 0;
            int count2 = 0;
            int textLength = text.Length;
            for (int j = 0; j < textLength; j++)
                if (text[j] == '\n')
                    count2++;
            return count2;
        }

        private void Align(RtfBuilder builder1, RtfBuilder builder2)
        {
            List<RtfBuilder.RtfPart> parts1 = new List<RtfBuilder.RtfPart> (builder1.Parts);
            List<RtfBuilder.RtfPart> parts2 = new List<RtfBuilder.RtfPart> (builder2.Parts);
            object[] tags1 = builder1.Tags;
            object[] tags2 = builder2.Tags;

           
            int currentIndex1 = 0;
            int currentIndex2 = 0;
            foreach (ComparableItem item in tags1)
            {
                var part1 = builder1.GetTaggedPart(item);
                var part2 = builder2.GetTaggedPart(item.RelatedItem);
                if (part1 != null && part2 != null)
                {
                    int currentLines1 = 0;
                    int currentLines2 = 0;
                    for (; currentIndex1 < parts1.Count; currentIndex1++)
                    {
                        if (parts1[currentIndex1] == part1)
                            break;
                        currentLines1 += CountLines(parts1[currentIndex1].Text);
                    }
                    for (; currentIndex2 < parts2.Count; currentIndex2++)
                    {
                        if (parts2[currentIndex2] == part2)
                            break;
                        currentLines2 += CountLines(parts2[currentIndex2].Text);
                    }

                    for (int i = currentLines1; i < currentLines2; i++)
                        parts1[currentIndex1 - 1].Text += "\r\n";
                    for (int i = currentLines2; i < currentLines1; i++)
                        parts2[currentIndex2 - 1].Text += "\r\n";
                    
                }
            }

        }


        private void GetRtf(ComparableItem item, RtfBuilder rtfBuilder, bool differencesOnly, bool useParentColor)
        {
            try
            {
                rtfBuilder.MaxTextSize = Settings.Current.MaxRichTextSize;
                item.GetAsRtf(rtfBuilder, differencesOnly, useParentColor);
                
            }
            catch (OverflowException)
            {
            }
            rtfBuilder.Flush();
        }

        private void ShowDetails(MainForm mainForm, ComparableItem item)
        {
            

            RtfBuilder diff1 = new RtfBuilder();
            RtfBuilder diff2 = new RtfBuilder();
            RtfBuilder orig1 = new RtfBuilder();
            RtfBuilder orig2 = new RtfBuilder();

            mainForm.SetProgress("Calculating text", 0, 5);
            GetRtf(item, diff1, true,false);
            mainForm.SetProgress("Calculating text", 1, 5);
            GetRtf(item.RelatedItem, diff2, true, false);
            mainForm.SetProgress("Calculating text", 2, 5);
            GetRtf(item, orig1, false, false);
            mainForm.SetProgress("Calculating text", 3, 5);
            GetRtf(item.RelatedItem, orig2, false, false);
            mainForm.SetProgress("Calculating text", 4, 5);

            Align(diff1, diff2);
            mainForm.SetProgress("Calculating text", 5, 5);
            
            ControlContainerForm<RichTextComparers> form = new ControlContainerForm<RichTextComparers>();
            form.Owner = mainForm;
            form.ShowOK = false;
            form.ShowCancel = false;
            form.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            form.Control.Original1 = orig1;
            form.Control.Original2 = orig2;
            form.Control.Difference1 = diff1;
            form.Control.Difference2 = diff2;
            mainForm.SetProgress("Calculating text", 6, 5);
            mainForm.ExecuteFunc ( form.ShowDialog, mainForm);

        }
        protected override void ExecuteAction(MainForm mainForm, ComparableItem[] focusedSelectedItems, ComparableItem[] unfocusedSelectedItems)
        {
            ThreadManager.Current.StartNew (()=> ShowDetails (mainForm , focusedSelectedItems.Last()));
            
        }
    }
}
